package com.guige.convert.service;

import com.guige.base.exception.vo.MessageVo;
import com.guige.base.util.JacobUtil;
import com.guige.base.util.StringUtils;
import com.guige.convert.model.dto.ParserFileDto;
import com.guige.convert.model.vo.ParserFileVo;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author songaw
 * @date 2018/5/17 9:52
 */
@Service
public class ConvertService {
    @Value("${filePath:}")
    String filePath;
    public MessageVo<ParserFileDto> convert(ParserFileVo parserFileVo,String type){

        ParserFileDto parserFileDto=new ParserFileDto();
        MessageVo messageVo = new MessageVo();
        List<ParserFileDto> parserFileDtos = new ArrayList<>();
        messageVo.setData(parserFileDtos);

        //保存到临时目录
        byte[] fileDate = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(parserFileVo.getBaseFile());
        InputStream input = new ByteArrayInputStream(fileDate);
        BeanUtils.copyProperties(parserFileVo,parserFileDto);
        parserFileDtos.add(parserFileDto);
        parserFileDto.setBaseFile(null);
        if (input==null) {
            parserFileDto.setBaseFile(null);
            messageVo.addErrorMessageObj("数据为空");

            return messageVo;
        }
        String fileName=parserFileVo.getFileName();
        String targetName="";

        fileName=fileName.substring(0, fileName.lastIndexOf("."));
        String houzhui = parserFileVo.getFileName().replace(fileName,"");
        targetName=fileName;

        fileName=fileName+"bak"+houzhui;



        Path srcFile=Paths.get(filePath,fileName);
        try {
            if (Files.exists(srcFile)) {

                Files.copy(input, srcFile, StandardCopyOption.REPLACE_EXISTING);
            } else {
                Files.createDirectories(srcFile);
                Files.copy(input,srcFile, StandardCopyOption.REPLACE_EXISTING);
            }

            Path targetFile=null;
            String name="";
            String suffix="";
            switch(type){
                case "htmlToExcel":

                    targetFile= Paths.get(filePath,targetName+".xlsx");
                    JacobUtil.htmlToExcel(srcFile.toString(),targetFile.toString());
                    break;
                case "docToPdf":
                    targetFile= Paths.get(filePath,targetName+".pdf");
                    JacobUtil.docToPdf(srcFile.toString(),targetFile.toString());
                    break;
                case "pptToPdf":
                    targetFile= Paths.get(filePath,targetName+".pdf");
                    JacobUtil.pptToPdf(srcFile.toString(),targetFile.toString());
                    break;
                case "excelToPDF":
                    targetFile= Paths.get(filePath,targetName+".pdf");
                    JacobUtil.excelToPDF(srcFile.toString(),targetFile.toString());
                    break;
            }

            if(targetFile==null||Files.exists(targetFile)){
                File f = targetFile.toFile();
                byte[] fileData=   FileUtils.readFileToByteArray(f);
                String data = new String(Base64.encodeBase64(fileData), "utf-8");
                parserFileDto.setBaseFile(data);
                parserFileDto.setFileName(targetFile.getFileName().toString());
                f.delete();
                srcFile.toFile().delete();
                messageVo.addInfoMessageObj("error.0002");


            }else{
                messageVo.addErrorMessageObj("error.0003");
            }
        }catch (Exception e){
            e.printStackTrace();
            parserFileDto.setBaseFile(null);
            messageVo.addErrorMessageObj("error.0003");
        }
        return messageVo;
    }

}
